﻿using Core.Tools.Config;
using Core.Tools.Extension;
using Core.Tools.Helper;
using Core.Tools.IOC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Core.Tools.Cache
{
    [ExposedService(Lifttime.Singleton,typeof(ICacheManager))]
    class CacheManager : ICacheManager
    {
        private IConfigManager configManager;
        private Dictionary<string,string> cacheNames;
        private readonly object LockCache = new object();

        public CacheManager(IConfigManager configManager)
        {
            this.configManager = configManager;
            cacheNames = configManager.Read<Dictionary<string, string>>(ConfigKey.CacheConfig) ?? new Dictionary<string, string>();
        }

        /// <summary>
        /// 获取本地缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Get<T>(ValueType key, out T value)
        {
            value= default(T);
            string o = key.ToString();

            lock (LockCache)
            {
                if(!cacheNames.ContainsKey(key.GetFullName()))
                {
                    return false;
                }    

                o = cacheNames[key.GetFullName()];  
            }

            try
            {
                value = JsonHelper.Deserialize<T>(o);
                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }


        /// <summary>
        /// 删除缓存内容
        /// </summary>
        /// <param name="key"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Remove(ValueType key)
        {
            lock (LockCache)
            {
                if (cacheNames.ContainsKey(key.GetFullName()))
                {
                    cacheNames.Remove(key.GetFullName());
                    configManager.Write(ConfigKey.CacheConfig, cacheNames);
                }
            }
        }

        /// <summary>
        /// 设置本地缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Set<T>(ValueType key, T value)
        {
            if (key == null) throw new ArgumentNullException("key");
            if (value == null) throw new ArgumentNullException("value");

            string content = JsonHelper.Serialize(value);

            lock (LockCache)
            {
                if(cacheNames.ContainsKey(key.GetFullName()))
                {
                    cacheNames[key.GetFullName()] = content;
                }
                else
                {
                    cacheNames.Add(key.GetFullName(), content);
                }

                configManager.Write(ConfigKey.CacheConfig, cacheNames);
            }
        }
    }
}
